<?php

/**
 * La classe de gestion des chemains
 *
 * PHP version 5
 *
 * LICENSE: GNU General Public License v3
 *
 * @category   Framework/Core
 * @package    Framework
 * @author     Mohammed Amine Kaabachi <jedevbien@gmail.com>
 * @copyright  2009 Authentic Development Group
 * @license    GNU General Public License v3
 * @since      File available since Release 0.1.0
 *
 */

class router
{
    private $_Route;
    private $_Controller;
    private $_Action;
    private $_Requests;
    private $_AdminRouting;

    public function __construct()
    {
        //on appel quelques class utiles (configs ...)
        $request =  core::load('request');
        $config = core::load('config');
        //on vérifie si il existe des routes personalisées
        if(file_exists(CONFIG_DIR.'routes.php')){
            include(CONFIG_DIR.'routes.php');
        }

        //on fragmente le chemin
        $requestURI = $request->getRequest('REQUEST_URI', 'Server');
        $validate_pattern = $config->url_chars;
        $route = preg_replace($validate_pattern, '', $requestURI);
        $route = str_replace('^', '', $requestURI);
        $route = str_replace('?', '', $requestURI);
        $requestURI = split('/', $route);
        $scriptName = split('/', $request->getRequest('SCRIPT_NAME', 'Server'));

        for($i= 0;$i < sizeof($scriptName);$i++)
        {
            if ($requestURI[$i]     == $scriptName[$i])
            {
                unset($requestURI[$i]);
            }
        }

        //on define un tableau qui contient le chemin fragmenté
        $_Route = array_values($requestURI);

        if (empty($_Route[0]))
        {
            $default_controller = $config->default_controller;
            if (!empty($default_controller))
            $_Route[0] = $default_controller;
            else
            $_Route[0] = 'index';
        }

        //on associe les partie fragmentées aux variables privées
        $routeParts = $_Route;
        $this->_Controller=$routeParts[0];
        $this->_Action=isset($routeParts[1])? $routeParts[1]:'index';

        //on configure le routing d'admin s'il existait
        if ($this->_Controller == 'admin' && !empty($this->_Action) && (!in_array($this->_Action, $config->admin_global_functions)))
        {
            $this->_AdminRouting = true;
            $this->_Controller = $routeParts[1];
            $this->_Action=!empty($routeParts[2])? $routeParts[2]:'index';

        }


        array_shift($routeParts);
        array_shift($routeParts);

        //on fragemente les parties restées pour avoir les requétes ($_Get)
        $newRouteParts = array();

        foreach (array_values($routeParts) as $routePart)
        {
            $routePart = split(':', $routePart);

            if (empty($routePart[0]))
            {
                continue;
            }
            else if (in_array($routePart[0], $newRouteParts))
            {
                unset($newRouteParts[$routePart[0]]);
            }

            if (isset($routePart[1]) && !empty($routePart[1]))
            {
                $newRouteParts[$routePart[0]] = $routePart[1];
            }
            else
            {
                $newRouteParts[$routePart[0]] = true;
            }
        }

        $this->_Requests = $newRouteParts;

        //on configure les routes personalisées s'ils existaient
        if (isset($routes))
        {
            foreach ($routes as $_route)
            {
                $_pattern = '~{$_route[0]}~';
                $_destination = $_route[1];

                if (preg_match($_pattern, $route))
                {
                    $newrouteparts = split('/', $_destination);
                    $this->_Controller = $newrouteparts[0];
                    $this->_Action = $newrouteparts[1];
                }

            }
        }
    }

    //fonction pour obtenir le type de routing
    public function getRoutingType()
    {
        if ($this->_AdminRouting == true)
        {
            return 2;
        }
        else
        {
            return 1;
        }
    }

    //fonction pour obtenir l'action
    public function getAction()
    {
        if (empty($this->_Action))
        {
            $this->_Action='index';
        }
        return $this->_Action;
    }

    //fonction pour obtenir le module
    public function getModule()
    {
        return $this->_Controller;
    }

    //fonction pour obtenir les controlleurs
    public function getController()
    {
        return $this->_Controller;
    }

    //fonction pour obtenir les paramétres
    public function getRequests()
    {
        return $this->_Requests;
    }

}
?>

